import sys, os
if hasattr(sys, 'frozen'):
    os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from dbutils import *
import os
from bot_log import BotLogger

logger = BotLogger(os.path.basename(__file__), logname=1).getLogger()


class Customer(object):

    def __init__(self, customer_id, compay_name, customer_code, tax_num, id_num, address, account_bank, email, status):
        self.customer_id = customer_id
        self.compay_name = compay_name
        self.customer_code = customer_code
        self.tax_num = tax_num
        self.id_num = id_num
        self.address = address
        self.account_bank = account_bank
        self.email = email
        self.status = status

def create_customer(cust):
    try:
        conn, cur = get_conn()
        if conn is None or cur is None:
            return None
        sql_str = 'INSERT INTO customer_code(customer_id, compay_name, customer_code, tax_num, id_num, address, account_bank, email, status) VALUES(?, ?, ?, ?, ?,?,?,?,?)'
        params = (cust.customer_id, cust.compay_name, cust.customer_code, cust.tax_num, cust.id_num, cust.address,
                  cust.account_bank, cust.email, cust.status
                  )
        cur.execute(sql_str, params)
        result = cur.fetchall()

        conn.commit()
        close_all(conn, cur)
        return result

    except sqlite3.Error as e:
        logger.error(str(e))


class Commodity(object):

    def __init__(self,commodity_id,commodity_name,commodity_code,model,unit,price,status):
        self.commodity_id = commodity_id
        self.commodity_name = commodity_name
        self.commodity_code = commodity_code
        self.model = model
        self.unit = unit
        self.price = price
        self.status = status

def create_commodity(commodity):
    try:
        conn, cur = get_conn()
        if conn is None or cur is None:
            return None
        sql_str = 'INSERT INTO commodity_code(commodity_id, commodity_name, commodity_code, model, unit, price, status) VALUES(?, ?, ?, ?, ?,?,?)'
        params = (commodity.commodity_id, commodity.commodity_name, commodity.commodity_code, commodity.model, commodity.unit, commodity.price,
                  commodity.status
                  )
        cur.execute(sql_str, params)
        result = cur.fetchall()

        conn.commit()
        close_all(conn, cur)
        return result

    except sqlite3.Error as e:
        logger.error(str(e))

class Sumupload(object):

    def __init__(self, upload_sum, reverse_writing_monitor, ukey_num):
        self.upload_sum = upload_sum
        self.reverse_writing_monitor = reverse_writing_monitor
        self.ukey_num = ukey_num

def create_sumupload(sum):
    try:
        conn, cur = get_conn()
        if conn is None or cur is None:
            return None

        sql_str = 'INSERT INTO sum_upload(upload_sum, reverse_writing_monitor,ukey_num) VALUES(?, ?, ?)'
        params = (sum.upload_sum, sum.reverse_writing_monitor, sum.reverse_writing_monitor)
        cur.execute(sql_str, params)
        result = cur.fetchall()

        conn.commit()
        close_all(conn, cur)
        return result

    except sqlite3.Error as e:
        logger.error(str(e))

def list_sumupload(upload_sum):
    try:
        conn, cur = get_conn()
        if conn is None or cur is None:
            return None
        cur.execute('SELECT ukey_num,reverse_writing_monitor FROM sum_upload WHERE upload_sum = ?', (upload_sum,))
        datalist = cur.fetchall()

        return datalist
    except sqlite3.Error as e:
        logger.error(str(e))

